<?php
// 当前页码
$page = request()->get('page',1);
$params = request()->get();
if($total<=$per_page) return '';

// 每页显示的记录数
$perPage = $per_page;
// 总记录数
$totalRecords = $total;



// 计算总页数
$totalPages = ceil($totalRecords / $perPage);

// 确保当前页码在有效范围内
$page = max(1, min($page, $totalPages));

// 计算当前页左侧和右侧显示的页码范围
$leftRange = max(1, $page - 3);
$rightRange = min($page + 3, $totalPages);



// 输出分页导航
echo "<nav aria-label='Page navigation'>";
echo "<ul class='pagination justify-content-end'>";



// 上一页按钮
if ($page > 1) {
    $params['page'] = $page - 1;
    echo "<li class='page-item'><a class='page-link' href='?".http_build_query($params)."'  title='上一页'>&laquo;</a></li>";
} else {
    echo "<li class='page-item disabled'><a class='page-link' href='#' title='上一页'>&laquo;</a></li>";
}

// 首页按钮
    if ($page > 4) {
        $params['page'] = 1;
        echo "<li class='page-item'><a class='page-link' href='?".http_build_query($params)."'>1</a></li>";
    }
// 左侧省略号
if ($leftRange > 1) {
    echo "<li class='page-item disabled'><a class='page-link' href='#'>...</a></li>";
}
// 分页数字按钮
for ($i = $leftRange; $i <= $rightRange; $i++) {
    if ($i == $page) {
        echo "<li class='page-item active'><a class='page-link' href='#'>{$i}</a></li>";
    } else {
        $params['page'] = $i;
        echo "<li class='page-item'><a class='page-link' href='?".http_build_query($params)."'>{$i}</a></li>";
    }
}
// 右侧省略号
if ($rightRange < $totalPages) {
    echo "<li class='page-item disabled'><a class='page-link' href='#'>...</a></li>";
}

// 尾页按钮
if ($page < $totalPages-3) {
    $params['page'] = $totalPages;
    echo "<li class='page-item'><a class='page-link' href='?".http_build_query($params)."'>$totalPages</a></li>";
} 

// 下一页按钮
if ($page < $totalPages) {
    $params['page'] = $page+1;
    echo "<li class='page-item'><a class='page-link' href='?".http_build_query($params)."' title='下一页'>&raquo;</a></li>";
} else {
    echo "<li class='page-item disabled'><a class='page-link' href='#' title='下一页'>&raquo;</a></li>";
}
echo "</ul>";
echo "</nav>";
?>